﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Shine.Util.Collections
{
    /// <summary>
    /// 
    /// </summary>
    public class ListHelper
    {
        /// <summary>
        /// 取得List集合中给定属性和值的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lists"></param>
        /// <param name="property"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T GetEnumeratorItem<T>(List<T> lists, string property, object value)
        {
            Predicate<T> predicate = delegate(T tempObj)
            {
                Type type = tempObj.GetType(); ;
                FieldInfo[] fieldInfos = type.GetFields();
                if (fieldInfos != null && fieldInfos.Length > 0)
                {
                    foreach (FieldInfo info in fieldInfos)
                    {
                        if (info.Name.Equals(property, StringComparison.CurrentCultureIgnoreCase))
                        {
                            return info.GetValue(tempObj).ToString() == value.ToString();
                        }
                    }
                }                
                PropertyInfo[] pinfos = type.GetProperties();
                if (pinfos != null && pinfos.Length > 0)
                {
                    foreach (PropertyInfo info in pinfos)
                    {
                        if (info.Name.Equals(property, StringComparison.CurrentCultureIgnoreCase))
                        {
                            return info.GetValue(tempObj, null).ToString() == value.ToString();
                        }
                    }
                }
                return false;
            };
            return lists.Find(predicate);
        }

        /// <summary>
        /// 取得List集合中给定属性和值的对象序列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lists"></param>
        /// <param name="property"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<T> GetEnumeratorItems<T>(List<T> lists, string property, object value)
        {
            Predicate<T> predicate = delegate(T tempObj)
            {
                Type type = tempObj.GetType(); ;
                FieldInfo[] fieldInfos = type.GetFields();
                PropertyInfo[] propertyInfos = type.GetProperties();
                FieldInfo fieldInfo = null;
                PropertyInfo propertyInfo = null;
                if (fieldInfos != null && fieldInfos.Length > 0)
                {
                    foreach (FieldInfo info in fieldInfos)
                    {
                        if (info.Name.Trim() == property.Trim())
                        {
                            fieldInfo = info;
                            break;
                        }
                    }
                    if (fieldInfo != null)
                    {
                        return
                         fieldInfo.GetValue(tempObj).ToString().ToUpper() == value.ToString().ToUpper();
                    }
                }
                if (propertyInfos != null && propertyInfos.Length > 0)
                {
                    foreach (PropertyInfo info in propertyInfos)
                    {
                        if (info.Name.Trim() == property.Trim())
                        {
                            propertyInfo = info;
                            break;
                        }
                    }
                    if (propertyInfo != null)
                    {
                        return
                         propertyInfo.GetValue(tempObj,null).ToString().ToUpper() == value.ToString().ToUpper();
                    }
                }
                return false;
            };
            return lists.FindAll(predicate);
            
        }
    }
}
